package com.lej.core.utils.extention

import android.app.Activity
import android.content.Context
import android.content.Intent
import androidx.activity.ComponentActivity
import androidx.activity.result.ActivityResult
import androidx.activity.result.contract.ActivityResultContracts
import androidx.fragment.app.Fragment
import com.alibaba.android.arouter.core.LogisticsCenter
import com.alibaba.android.arouter.facade.Postcard
import com.alibaba.android.arouter.facade.service.PretreatmentService
import com.alibaba.android.arouter.launcher.ARouter
import com.alibaba.android.arouter.utils.TextUtils
import com.lej.core.base.currentActivity
import com.lej.core.common.TITLE
import com.lej.core.common.WEB_SHOW_TITLE
import com.lej.core.common.WEB_URL
import com.lej.core.router.CRouter

fun Context.toIntent(route: String, block: Postcard.() -> Unit = {}) =
    ARouter.getInstance().build(route)
        .withTransition(android.R.anim.fade_in, android.R.anim.fade_out)
        .also(block)
        .toActivityIntent(this)

private fun Postcard.toActivityIntent(context: Context): Intent? {
    val pretreatmentService = ARouter.getInstance().navigation(PretreatmentService::class.java)
    if (pretreatmentService?.onPretreatment(context, this) != true) {
        return null
    }
    kotlin.runCatching { LogisticsCenter.completion(this) }
    val intent = Intent(context, destination)
    intent.putExtras(extras)
    val flags: Int = flags
    if (-1 != flags) {
        intent.flags = flags
    } else if (context !is Activity) {    // Non activity, need less one flag.
        intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
    }
    val action: String? = action
    if (!TextUtils.isEmpty(action)) {
        intent.action = action
    }
    return intent
}

fun Activity?.router(route: String, reqCode: Int = -1, block: Postcard.() -> Unit = {}) {
    ARouter.getInstance().build(route)
        .withTransition(android.R.anim.fade_in, android.R.anim.fade_out)
        .also(block)
        .navigation(this ?: currentActivity, reqCode)
}

fun Fragment?.router(route: String, reqCode: Int = -1, block: Postcard.() -> Unit = {}) {
    val act = this?.activity ?: currentActivity
    ARouter.getInstance().build(route)
        .withTransition(android.R.anim.fade_in, android.R.anim.fade_out)
        .also(block)
        .navigation(act, reqCode)
}

fun Activity?.toLink(
    url: String,
    title: String? = null,
    showTitle: Boolean = false,
    block: Postcard.() -> Unit = {}
) {
    ARouter.getInstance().build(CRouter.webView)
        .withString(WEB_URL, url)
        .withString(TITLE, title)
        .withBoolean(WEB_SHOW_TITLE, showTitle)
        .withTransition(android.R.anim.fade_in, android.R.anim.fade_out)
        .also(block)
        .navigation(this ?: currentActivity)
}

fun Fragment?.toLink(
    url: String,
    title: String? = null,
    showTitle: Boolean = false,
    block: Postcard.() -> Unit = {}
) {
    (this?.activity ?: currentActivity).toLink(url, title, showTitle, block)
}

//添加移除历史activity的flag
fun Postcard.addClearFlag() = also {
    withFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK)
}

/**依据route路由获取fragment实例*/
fun String.getFragmentByRoute() =
    ARouter.getInstance().build(this).navigation() as Fragment


fun ComponentActivity.registerIntent(block: (ActivityResult) -> Unit) =
    registerForActivityResult(ActivityResultContracts.StartActivityForResult(), block)